package com.lianbang.pharmacy.vm

import androidx.lifecycle.MutableLiveData
import cn.ubest.mvvmcore.base.BaseListViewModel
import cn.ubest.mvvmcore.base.BaseViewModel
import cn.ubest.mvvmcore.bean.ListDataLoader
import cn.ubest.mvvmcore.interfaces.RequestHandler1Impl
import com.lianbang.pharmacy.bean.MedicineCategoryListBean
import com.lianbang.pharmacy.bean.MedicineListBean
import com.lianbang.pharmacy.model.MedicineModel

class MedicineVM : BaseListViewModel<MedicineModel>() {
    val mMedicineCategoryLiveData by lazy {
        MutableLiveData<List<MedicineCategoryListBean>>()
    }

    override fun loadData(isInit: Boolean, vararg arg: Any) {
        val word = arg[0]
        if (word is String) {
            requestWithLife(object : RequestHandler1Impl<List<MedicineListBean>>(this) {
                override suspend fun request1(): List<MedicineListBean> {
                    return mModel.searchMedicine(word, 2).resolveWrapper()
                }

                override fun onSuccess(t: List<MedicineListBean>) {
                    setData(t)
                }
            }, isInit)
        }
    }

    fun medicineCategoryList() {
        requestWithLife(object : RequestHandler1Impl<List<MedicineCategoryListBean>>(this) {
            override suspend fun request1(): List<MedicineCategoryListBean> {
                return mModel.medicineCategoryList().resolveWrapper()
            }

            override fun onSuccess(t: List<MedicineCategoryListBean>) {
                mMedicineCategoryLiveData.postValue(t)
            }
        })
    }
}